//index.js
//获取应用实例
var app = getApp();
//获得

var MyRequest = require("../../utils/MyRequest.js");
var Global = require("../../Global.js");
var Cart = require("../../LocalCart.js");

Page({
    data: {
        commonClass: "tab-show",
        cardClass: 'tab-hide',
        commonState: "show",
        cardState: "hide"
    },
    showCommon: function () {
        if (this.data.commonClass == "tab-show") {
            return;
        }
        this.setData({
            commonClass: "tab-show",
            cardClass: 'tab-hide',
            commonState: "show",
            cardState: "hide"
        });
    },
    showCard: function () {
        if (this.data.cardClass == "tab-show") {
            return;
        }
        this.setData({
            commonClass: "tab-hide",
            cardClass: 'tab-show',
            commonState: "hide",
            cardState: "show"
        });
    },
    onLoad: function (options) {
        this.setData({
            balance: options.balance
        });
    },
    balanceInput: function (e) {
        this.setData({
            totalPrice: e.detail.value
        });
    },
    getInfo: function () {
        // if(this.data.totalPrice<1){
        //                    wx.showToast({ title: '金额最低为1元', icon: 'loading', duration: 2000 })
        //     return;
        // }

        var data =
            {
                orderType: 0,
                userId: Global.getUserStorage().id,
                money: this.data.totalPrice,
                cityId: Global.getUserStorage().cityId,
                platform: Global.getPlatform(),
                version: Global.getVersion(),
                wechatOpenid: Global.getOpenId(),
            };
        MyRequest.MyGetWithToast("PayBean", this, this.infoBack, data);
    },
    infoBack: function (result, that) {
        
        var paybean = result.data.data.weiPay;
        wx.requestPayment({
            appId: paybean.appId,
            timeStamp: paybean.timeStamp,
            nonceStr: paybean.nonceStr,
            package: "prepay_id=" + paybean.prepayId,
            signType: 'MD5',
            paySign: paybean.sign,
            success: function (res) {
                wx.navigateBack({
                    delta: 1
                })
            },
            fail: function (res) {
                wx.showToast({ title: '充值失败', icon: 'loading', duration: 2000 })
            },
            complete:function(data){
                console.log(data);
            }
        })
    },
    bindCardNo: function (e) {
        this.setData({
            cardNo: e.detail.value
        });
    },
    bindCardPW: function (e) {
        this.setData({
            cardPW: e.detail.value
        });
    },
    bindCard: function () {
        if (this.data.cardNo == undefined || this.data.cardNo == "") {
            wx.showToast({
                title: '请输入卡号',
                duration: 2000
            })
            return;
        }
        if (this.data.cardPW == undefined || this.data.cardPW == "") {
            wx.showToast({
                title: '请输入密码',
                duration: 2000
            })
            return;
        }

        var data = {
            action: "prepaid",
            card: this.data.cardNo,
            password: this.data.cardPW,
            accountId: Global.getUserStorage().id
        }
        console.log(data);
        MyRequest.MyGetWithToast("CardBalance", this, function (result, that) {
            wx.navigateBack({
                delta: 1
            })
        }, data);
    }
});
